package com.achun.getcouponleague.ui.adapter;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.achun.getcouponleague.Bean.SelectCategories;
import com.achun.getcouponleague.R;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.UIutils;

import java.util.ArrayList;
import java.util.List;

public class SelectCategoryAdapter extends RecyclerView.Adapter<SelectCategoryAdapter.innerholder> {
    private List<SelectCategories.DataBean> dataBeanList=new ArrayList<>();
    private int currentitme=0;
    @NonNull
    @Override
    public innerholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView textView = new TextView(parent.getContext());
        textView.setTextSize(20);
        int size = UIutils.dip2px(parent.getContext(), 10);
        textView.setPadding(size,size, 0,size);
        textView.setLayoutParams(layoutParams);

        return new innerholder(textView);
    }

    @Override
    public void onBindViewHolder(@NonNull innerholder holder, int position) {
        int clickposition=position;
        TextView itemView = (TextView) holder.itemView;
        itemView.setText(dataBeanList.get(position).getFavorites_title());
        if (clickposition==currentitme){
            itemView.setBackgroundResource(R.color.main_back);
        }else {
            itemView.setBackgroundColor(Color.WHITE);
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentitme!=clickposition&&onLeftListClickListenter!=null) {
                    currentitme = clickposition;
                    Logutils.e(this.getClass(), "点击了--" + clickposition);
                    onLeftListClickListenter.onclick(dataBeanList.get(clickposition));
                    notifyDataSetChanged();
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public void setDatas(SelectCategories selectCategories) {
        List<SelectCategories.DataBean> data = selectCategories.getData();
        dataBeanList.clear();
        dataBeanList.addAll(data);
        notifyDataSetChanged();
//根据点击加载第一个
        if (dataBeanList.size()>0){
            onLeftListClickListenter.onclick(dataBeanList.get(currentitme));
        }
    }

    public class innerholder extends RecyclerView.ViewHolder {
        public innerholder(@NonNull TextView itemView) {
            super(itemView);
        }

    }
    public interface OnLeftListClickListenter{
        void onclick(SelectCategories.DataBean bean);
    }
    private OnLeftListClickListenter onLeftListClickListenter;
    public void setOnLeftListClickListenter(OnLeftListClickListenter onLeftListClickListenter){

        this.onLeftListClickListenter=onLeftListClickListenter;
    }
}
